//
//  FriendCircleCell.m
//  ReactCocoaDemo
//
//  Created by letian on 16/12/5.
//  Copyright © 2016年 cmsg. All rights reserved.
//

#import "FriendCircleCell.h"
#import "FriendCircleImageView.h"
#import "FriendCircleModel.h"
#import "FriendCircleCommentView.h"
#import "LTCommentCell.h"
#import "UILabel+Extension.h"
#import "FriendCircleCommentModel.h"

@interface FriendCircleCell () <UITableViewDelegate, UITableViewDataSource>

/** <#des#> */
@property (nonatomic,strong) UIImageView * iconView;

/** <#des#> */
@property (nonatomic,strong) UILabel * nameLabel;

@property (nonatomic,strong) UIView *startView;

@property (nonatomic,strong) UILabel *timeLbl;

@property (nonatomic,strong) UILabel *shopInfoLbl;

@property (nonatomic,strong) UIView *cutViewl;

/** <#des#> */
@property (nonatomic,strong) UILabel * contentLabel;

/** 全文/收起 */
@property (nonatomic,strong) UIButton * allContentButton;

/** <#des#> */
@property (nonatomic,strong) FriendCircleImageView * friendCircleImageView;

/** <#des#> */
@property (nonatomic,strong) FriendCircleModel * model;

/** <#des#> */
@property (nonatomic,strong) FriendCircleCommentView * commentView;

/** <#des#> */
@property (nonatomic,copy) dispatch_block_t btClickBlock;

@property (nonatomic,strong) UITableView *tableView;

@end

@implementation FriendCircleCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
        [self setConstant];
        
    }
    
    return self;
}

#pragma mark - 设置UI
- (void)setupUI
{
    self.iconView = [LTUITools lt_creatImageView];
    [self.contentView addSubview:self.iconView];
    
    self.nameLabel = [LTUITools lt_creatLabel];
    [self.contentView addSubview:self.nameLabel];
    
    self.startView = [LTUITools lt_creatView];
    self.startView.backgroundColor = [UIColor blueColor];
    [self.contentView addSubview:self.startView];
    
    self.timeLbl = [LTUITools lt_creatLabel];
    self.timeLbl.backgroundColor = [UIColor redColor];
    self.timeLbl.text = @"2017-01-03";
    [self.contentView addSubview:self.timeLbl];
    
    self.shopInfoLbl = [LTUITools lt_creatLabel];
    self.shopInfoLbl.text = @"颜色分类:666   尺码:165(L)";
    self.shopInfoLbl.textColor = [UIColor blackColor];
    self.shopInfoLbl.backgroundColor = [UIColor greenColor];
    [self.contentView addSubview:self.shopInfoLbl];
    
    self.cutViewl = [LTUITools lt_creatView];
    [self.contentView addSubview:self.cutViewl];
    
    
    
    self.contentLabel = [LTUITools lt_creatLabel];
    self.contentLabel.backgroundColor = [UIColor yellowColor];
    [self.contentView addSubview:self.contentLabel];
    
    self.allContentButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.allContentButton.backgroundColor = [UIColor redColor];
    [self.allContentButton setTitle:@"查看全部" forState:UIControlStateNormal];
    [self.allContentButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    self.allContentButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
    self.allContentButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.contentView addSubview:self.allContentButton];
    
    self.friendCircleImageView = [FriendCircleImageView new];
    self.friendCircleImageView.backgroundColor = [UIColor blueColor];
    [self.contentView addSubview:self.friendCircleImageView];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollEnabled = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIImage *image = [[[UIImage imageNamed:@"LikeCmtBg"] stretchableImageWithLeftCapWidth:40 topCapHeight:30] imageWithRenderingMode:UIImageRenderingModeAutomatic];
    self.tableView.backgroundView = [[UIImageView alloc] initWithImage:image];
    [self.contentView addSubview:self.tableView];
    
    [self.tableView registerClass:[LTCommentCell class] forCellReuseIdentifier:@"LTCommentCell"];

    
    [self.allContentButton bk_whenTapped:^{
        self.model.select = !self.model.select;
        if (self.btClickBlock) {
            self.btClickBlock();
        }
    }];
}

#pragma mark - 设置约束
- (void)setConstant
{
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(20);
        make.left.equalTo(self.contentView).offset(10);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconView.mas_right).offset(8);
        make.right.equalTo(self.contentView).offset(-10);
        make.height.mas_equalTo(20);
        make.top.equalTo(self.iconView).offset(5);
    }];
    
    [self.startView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel);
        make.top.equalTo(self.nameLabel.mas_bottom).offset(5);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(100);
    }];
    
    [self.timeLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.iconView.mas_bottom).offset(-10);
        make.right.equalTo(self.contentView).offset(-10);
    }];
    
    [self.shopInfoLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconView.mas_bottom).offset(20);
        make.left.equalTo(self.iconView);
        make.height.mas_equalTo(16);
        make.right.equalTo(self.contentView).offset(-10);
    }];
    
    [self.cutViewl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.shopInfoLbl.mas_bottom).offset(20);
        make.left.equalTo(self.shopInfoLbl);
        make.right.equalTo(self.shopInfoLbl).offset(-10);
        make.height.mas_equalTo(1);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.cutViewl.mas_bottom).offset(10);
        make.left.equalTo(self.contentView).offset(10);
        make.right.equalTo(self.contentView).offset(-10);
    }];
   
    
#pragma mark - friendCircleImageView每部已经自动自动计算高度
    [self.friendCircleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.mas_bottom).offset(10);
        make.right.left.equalTo(self.cutViewl);
    }];
    
    
    [self.allContentButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.friendCircleImageView.mas_bottom).offset(10);
        make.left.equalTo(self.cutViewl);
        make.height.mas_equalTo(16);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.allContentButton.mas_bottom).offset(10);
        make.left.right.equalTo(self.cutViewl);
        make.height.mas_equalTo(0);
        make.bottom.equalTo(self.contentView).offset(-10);
    }];
    
}

- (void)configConstant
{
    CGFloat height = 0;
    if (self.model.commentDatas.count > 0) {
       height  = self.tableView.contentSize.height;
    }
    
    
    self.allContentButton.hidden = (self.model.commentDatas.count<4);
    
    [self.contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.cutViewl.mas_bottom).offset(10);
        make.left.equalTo(self.contentView).offset(10);
        make.right.equalTo(self.contentView).offset(-10);
    }];
    
    if (self.model.images && self.model.commentDatas.count > 3) {
        
        [self.friendCircleImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentLabel.mas_bottom).offset(10);
            make.right.left.equalTo(self.cutViewl);
        }];
        
        [self.allContentButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.friendCircleImageView.mas_bottom).offset(10);
            make.left.equalTo(self.cutViewl);
            make.height.mas_equalTo(16);
        }];
        
        
        
        [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.allContentButton.mas_bottom).offset(10);
            make.left.right.equalTo(self.cutViewl);
            make.height.mas_equalTo(height);
            make.bottom.equalTo(self.contentView).offset(-10);
        }];
        
    } else if( !self.model.images && self.model.commentDatas.count > 3){
        
        [self.friendCircleImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
     
        }];
        
        [self.allContentButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentLabel.mas_bottom).offset(10);
            make.left.equalTo(self.cutViewl);
            make.height.mas_equalTo(16);
        }];
        
        [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.allContentButton.mas_bottom).offset(10);
            make.left.right.equalTo(self.cutViewl);
            make.height.mas_equalTo(height);
            make.bottom.equalTo(self.contentView).offset(-10);
        }];
        
    } else if( self.model.images && self.model.commentDatas.count > 0){
        
        [self.friendCircleImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentLabel.mas_bottom).offset(10);
            make.right.left.equalTo(self.cutViewl);
        }];
        
        [self.allContentButton mas_remakeConstraints:^(MASConstraintMaker *make) {
           
        }];
        
        [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.friendCircleImageView.mas_bottom).offset(10);
            make.left.right.equalTo(self.cutViewl);
            make.height.mas_equalTo(height);
            make.bottom.equalTo(self.contentView).offset(-10);
        }];
        
    } else if( !self.model.images && self.model.commentDatas.count > 0){
        
        [self.friendCircleImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
           
        }];
        
        [self.allContentButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            
        }];
        
        [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentLabel.mas_bottom).offset(10);
            make.left.right.equalTo(self.cutViewl);
            make.height.mas_equalTo(height);
            make.bottom.equalTo(self.contentView).offset(-10);
        }];
        
    } else if( self.model.images && self.model.commentDatas.count <= 0) {
        
        [self.friendCircleImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentLabel.mas_bottom).offset(10);
            make.right.left.equalTo(self.cutViewl);
            make.bottom.equalTo(self.contentView).offset(-10);
        }];
        
        [self.allContentButton mas_remakeConstraints:^(MASConstraintMaker *make) {

        }];
        [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
        }];

        
    } else if( !self.model.images && self.model.commentDatas.count <= 0) {
        
        [self.friendCircleImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
        }];
        
        [self.allContentButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            
        }];
        [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            
        }];
        
        [self.contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.cutViewl.mas_bottom).offset(10);
            make.right.left.equalTo(self.cutViewl);
            make.bottom.equalTo(self.contentView).offset(-10);
        }];
    } else {
        NSLog(@"条件筛选错误");
    }
   
    [self.friendCircleImageView cellDataWithImageArray:self.model.images];
}

- (void)cellDataWithModel:(FriendCircleModel *)model
{
    self.model = model;
   
    
    self.nameLabel.text = [NSString stringWithFormat:@"%@ %@",model.name,model.personalizedSignature];
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:model.icon]];
    self.contentLabel.text = model.content;
    [self.contentLabel sizeToFit];
    [self.friendCircleImageView cellDataWithImageArray:model.images];
    self.timeLbl.text = model.time;
    self.shopInfoLbl.text = @"颜色分类:666   尺码:165(L)";
    [self.tableView reloadData];
    
    
    [self configConstant];

}


- (void)cellClickBt:(dispatch_block_t)clickBtBlock
{
    self.btClickBlock = clickBtBlock;
}

#pragma mark - tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (!self.model.commentDatas) {
        return 0;
    } else if (self.model.commentDatas.count >3) {
        if (self.model.select == YES) {
            return self.model.commentDatas.count;
        } else {
            return 3;
        }
    } else {
        return self.model.commentDatas.count;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FriendCircleCommentModel *model = self.model.commentDatas[indexPath.row];
    
    return model.cellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LTCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LTCommentCell"];
    BOOL idHidden = ((indexPath.row +1) == self.model.commentDatas.count);
    [cell cellDataWithModle:self.model.commentDatas[indexPath.row] andIshidden:idHidden];
    return cell;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

@end
